//
//  ProjectRiskViewController.swift
//  cxd4iphone
//
//  Created by hexy on 12/3/15.
//  Copyright © 2015 hexy. All rights reserved.
//

import UIKit

let BidRiskHeaderViewReusedId = "BidRiskHeaderViewReusedId"

class BidRiskViewController: BaseTableViewController {

    private lazy var bidRiskListViewModel = BidRiskListViewModel()
    
    var bidViewModel = BidViewModel(bidModel: BidModel(dict: ["":""]))
    
    init(bidViewModel: BidViewModel) {
        
        
        super.init(style: .Plain)
        self.title = "项目风控"
        self.bidViewModel = bidViewModel
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        loadData()
    }
    
    private func loadData() {
        
        for bidDescribeModel in bidViewModel.bidModel.listWindControlList! {
            
            bidRiskListViewModel.sections.append([BidRiskCellViewModel(bidDescribeModel: bidDescribeModel)])
        }
        
        xx_print(bidViewModel.bidModel.loanWindControlPicUrls)
        self.tableView.reloadData()
    }
    
    override func makeTableView() {
        
        
        self.tableListViewModel = bidRiskListViewModel
        
        tableView.registerClass(BidRiskListCell.self, forCellReuseIdentifier: BidRiskCellReuseID)
        
        tableView.registerClass(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: BidRiskHeaderViewReusedId)
        
        tableView.sectionHeaderHeight = xx_height(30)
        
    }


}

extension BidRiskViewController {
    
    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let vm = bidRiskListViewModel.sections[section][0] as! BidRiskCellViewModel
        
        let view = tableView.dequeueReusableHeaderFooterViewWithIdentifier(BidRiskHeaderViewReusedId)
        
        view!.textLabel?.text = vm.bidDescribeModel.key
        return view
    }

    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

        let viewModel = bidRiskListViewModel.sections[indexPath.section][indexPath.row] as! BidRiskCellViewModel

        if viewModel.rowHeight > 0 {
            return viewModel.rowHeight
        }

        let cell = tableView.dequeueReusableCellWithIdentifier(viewModel.reuseId) as! BidRiskListCell

        viewModel.rowHeight = cell.rowHeight(viewModel)

        return viewModel.rowHeight
    }
}
